<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_weishopwater/model')->方法名();
*/
namespace jy_weishopwater;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function check_ya($goodsid=0,$getfee=false){
		$groupid=pdo_getcolumn('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$goodsid),'pcate');
		$yalog=pdo_get('jy_weishopwater_yalog',array('uniacid'=>$_SESSION['uniacid'],'gid'=>$groupid,'mid'=>$_SESSION['uid']));
		if($yalog['status']==1){
			if($getfee){
				return 0;
			}else{
				return true;
			}
		}else{
			if($getfee){
				$ya_fee=pdo_getcolumn('jy_weishopwater_goods',array('uniacid'=>$_SESSION['uniacid'],'groupid'=>$groupid),'ya_fee');
				return $ya_fee?:0;
			}else{
				return false;
			}
		}
	}
	public function up_log($order=array(),$getfee=false){
		$api_data=json_decode($order['api_data'],true);
		if(!$api_data['yafee']){
			return false;
		}
		$goods=pdo_getall('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$order['id']));
		foreach($goods as $item){
			$groupid=pdo_getcolumn('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['goodsid']),'pcate');
			$yagoods=pdo_get('jy_weishopwater_goods',array('uniacid'=>$_SESSION['uniacid'],'groupid'=>$groupid));
			if($yagoods['id']){
				$yalog=pdo_get('jy_weishopwater_yalog',array('uniacid'=>$_SESSION['uniacid'],'gid'=>$groupid,'mid'=>$_SESSION['uid']));
				if($yalog['status']!=1){
					$this->addlog($yagoods);
				}
				break;
			}
		}
		return true;
	}
	public function addlog($yagoods=array(),$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'fee'=>$yagoods['ya_fee'],
			'gid'=>$yagoods['groupid'],
			'status'=>1,
			'ispay'=>1,
			'createtime'=>TIMESTAMP,
		);
		$this->pdo_insert('yalog',$data);
		return true;
	}
	/*
		退款打款
	*/
	public function refund_pay($refund_id,$noexi=false,$isteam=false,$paytype='',$transaction_id='',$mid='',$price=''){
		//执行退款操作
		$refund=pdo_get('jy_weishopwater_yalog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$refund_id));
		if(!$refund){
			return false;
		}
		if($refund['status']!=3){
			if($noexi){
				return false;
			}
			exi('退款单不符合打款要求！','error');
		}
		$refund['price']=$price?:$refund['fee'];
		$refund['mid']=$mid?:$refund['mid'];
		$payinfo=pdo_get('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['pay_id']));
		if(!$transaction_id){
			$transaction_id=$refund['transid']?:$payinfo['transaction_id'];
		}
		$refund['paytype']=$paytype?:$payinfo['pay_type'];
		$refund['paytype']=$refund['paytype']?:'credit';
		$money=$refund['price'];
		if($payinfo['di_fee']>0){
			$payinfo['fee'] -=$payinfo['di_fee'];
			$money -=$payinfo['di_fee'];
		}
		//支付方式为余额支付，退款至虚拟币
		if($refund['paytype']=='credit' || $refund['paytype']=='apipay'){
			$pay_out_id=model('core/member')->add_credit2($refund['mid'],$money,'水桶押金退款','jy_weishopwater');
		}elseif(strpos($refund['paytype'],'credit') !== false){
			$creditfee=$order['creditfee']?:$money;
			$pay_out_id=model('core/member')->member_credit($refund['mid'],$refund['paytype'],$creditfee,true,'','水桶押金退款');
		}elseif($refund['paytype']=='wechat'){
			if(!$transaction_id){
				if($noexi){
					return false;
				}
				exi('退款失败，没有微信支付号！','error');
			}
			$payinfo['transid']=$transaction_id;
			$payinfo['money']=$money;
			$payinfo['remark']='水桶押金退款';
			$pay_out_id=model('core/member')->refund_out($refund['mid'],$payinfo,$noexi);
		}elseif($refund['paytype']=='alipay'){
			if(!$transaction_id){
				if($noexi){
					return false;
				}
				exi('退款失败，没有支付宝支付号！','error');
			}
			$params=array(
				'money'=>$money,
				'ordersn'=>$payinfo['tid'],
				'transid'=>$transaction_id,
				'remark'=>'水桶押金退款',
			);
			$pay_out_id=model('core/member')->refund_out($refund['mid'],$params,$noexi,'alipay');
		}else{
			$params=array(
				'money'=>$money,
				'ordersn'=>$payinfo['tid'],
				'remark'=>'水桶押金退款',
			);
			$pay_out_id=model('core/member')->pay_out($refund['mid'],$params,$noexi);
		}
		$update=array(
			'status'=>2,
			'paytime'=>TIMESTAMP,
			'pay_out_id'=>$pay_out_id,
		);
		pdo_update('jy_weishopwater_yalog',$update,array('id'=>$refund_id));
		if($payinfo['di_fee']>0){
			cfc('member')->member_credit($payinfo['mid'],$payinfo['di_credit'],$payinfo['di_fee'],true,'','退款：'.$payinfo['title']);
		}
		return true;
	}
}
?>